using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
//using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;
using TDBaseDataLibrary;
namespace TDEngineLibrary
{
    class TDWaveWorldGod : TDWorldGod
    {
        TDLevelConfig m_config;
        double m_dgentime;
        int m_nCurWave;
        int m_nCurCount;
        

        public TDWaveWorldGod()
        {
            m_dgentime = 1000;
            m_nCurWave = 0;
           
        }
        public void SetGodConfig(TDLevelConfig config)
        {
            m_config = config;
            m_nCurCount = m_config.wavelist[m_nCurWave].count;
        }
        public override void Reset()
        {
            m_dgentime = 1000;
            m_nCurWave = 0;
            m_nCurCount = m_config.wavelist[m_nCurWave].count;
        }

        public override int Update(GameTime time, int OffsetX, int OffsetY)
        {
            if (m_nCurWave > 0 || m_nCurCount < 20)
                return 1;
            m_dgentime -= time.ElapsedGameTime.TotalMilliseconds;

            if (m_dgentime < 0)
            {
                if (m_nCurWave >= m_config.wavelist.Count)
                    return 1;
                TDWaveInfo wave = m_config.wavelist[m_nCurWave];
                while (m_nCurCount <= 0 && m_nCurWave < m_config.wavelist.Count)
                {
                    m_nCurWave++;
                    wave = m_config.wavelist[m_nCurWave];
                    m_nCurCount = wave.count;
                }
                if (m_nCurWave >= m_config.wavelist.Count)
                    return 1;
                Enemy e = new Enemy(wave.name, null, (float)time.TotalGameTime.TotalMilliseconds);
                e.AddGene(TDCharacterInfo.m_RaceGene.Races[0]);
                e.ResetStatus();
                TDGotoTask task = new TDGotoTask();

                Vector2 vpos = new Vector2(0, 0);
                Vector2 vposend = new Vector2(792, 464);
                e.SetWorld(m_World);
                e.SetPosition(vpos);
                m_World.AddSprite(e);
                task.CreateGoToTask(e, vposend,"a*");
                e.AddTask(task, true);
                m_dgentime += 1000;
                m_nCurCount--;
                if (wave.count <= 0)
                    m_nCurWave++;
            }
            return 1;
        }
    }
    public class TDLevel : TDBaseLevel
    {
        public TDLevel(Game game)
            : base(game) { }
        ~TDLevel() { }

        bool m_bLButtonDown = false;
        bool m_bRButtonDown = false;
        bool m_bLHitControl = false;


        public TDLevelConfig config;
        public static TDWorld m_World;
        public ControlPal m_ControlPal;
        public PropertyPal m_PropertyPal;

        public SpriteDef[]  m_spriteDef;
        public GameSprite   m_gsprite;



        public override bool Init()
        {
            return true;
        }
        public int Reset()
        {
            m_World.ResetWorld();
            return 1;
        }
        public override void LoadContent(ContentManager Content)
        {
            EnemySprite.liftbound = Content.Load<Texture2D>("liftbound");
            EnemySprite.liftshow = Content.Load<Texture2D>("liftshow");
            Texture2D texexplosion = Content.Load<Texture2D>("Explosion");
            Vector2 vExCenter = new Vector2(40, 40);
            Enemy.LoadContent(Content);
            Enemy.Explosion = new GameSprite(texexplosion, 200, true, 80, 1600, 20, vExCenter);
            if (m_World == null)
            {
                m_World = new TDWorld();
                m_World.LoadContent(Content);
            }
            if (m_ControlPal == null)
            {
                m_ControlPal = new ControlPal();
                m_ControlPal.LoadContent(Content);
            }
            if (m_PropertyPal == null)
            {
                m_PropertyPal = new PropertyPal();
                m_PropertyPal.LoadContent(Content);
                
            }
            config = Content.Load<TDLevelConfig>("Levels/confused_config");
            TDWaveWorldGod worldgod = new TDWaveWorldGod();
            worldgod.SetGodConfig(config);
            m_World.SetGod(worldgod);
            m_World.LoadWorld(config.worldname);
        }
        public override void UnloadContent()
        {
        }
        public bool LoadLevel(ContentManager Content,String szLevelConfig)
        {
            config = Content.Load<TDLevelConfig>(szLevelConfig);
            TDWaveWorldGod worldgod = new TDWaveWorldGod();
            worldgod.SetGodConfig(config);
            m_World.SetGod(worldgod);
            m_World.LoadWorld(config.worldname);
            return true;
        }
        public override int UpdateInput(GameTime time, int OffsetX, int OffsetY)
        {
            return 1;
        }
        public override int Update(GraphicsDeviceManager graphics, GameTime gameTime)
        {
            if (base.Update(graphics, gameTime) == 1)
                return 1;
            if (m_ControlPal.UpdateInput(gameTime) != 1)
            {
                if (m_PropertyPal.UpdateInput(gameTime) != 1)
                {
                    if (m_World.UpdateInput(gameTime, 0, 0) == 1)
                    {
                        if (m_World.m_nSelectEnemy >= 0)
                        {
                            m_PropertyPal.m_nLift = (int)m_World.m_enemyList[m_World.m_nSelectEnemy].GetStatus(enumstatus.status_lift);
                        }
                    }
                }
            }
            else
                m_World.m_nOperate = m_ControlPal.GetSelectTowner();

            m_ControlPal.Update(gameTime);

            if (m_World.m_nOperate == 5)
            {
               Reset();
               m_World.m_nOperate = -1;
            }
            int nRet = m_World.Update(gameTime, 0, 0);
            
            m_ControlPal.SetSelectTowner(m_World.m_nOperate);
            return nRet;
        }
        public override void Draw(GraphicsDeviceManager graphics, GameTime gameTime, SpriteBatch spriteBatch, int xOffset, int yOffset, float fScale)
        {
            spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend);
            m_World.Draw(graphics, gameTime, spriteBatch, xOffset, yOffset, fScale);
            m_ControlPal.Draw(graphics, gameTime, spriteBatch, xOffset, yOffset);
            if(m_World.m_nSelectEnemy >= 0)
                m_PropertyPal.Draw(graphics, gameTime, spriteBatch, xOffset, yOffset);
            base.Draw(graphics, gameTime, spriteBatch, xOffset, yOffset,fScale);
            spriteBatch.End();

            //float fWidth = GameGlobalInfo.m_MouseTex.Width * fScale;
            //float fHeight = GameGlobalInfo.m_MouseTex.Height * fScale;
            //Rectangle rtMouse = new Rectangle((int)(state.X - fWidth / 2), (int)(state.Y - fHeight / 2), (int)(fWidth), (int)(fHeight));

            //spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
            //spriteBatch.Draw(GameGlobalInfo.m_MouseTex, rtMouse, Color.White);
            //spriteBatch.End();
        }
    }
}
